<template>
	<view class="page page-pickupsale">
		<view p10px>
			<uni-title type="h3" :title="`${type? '寄售':'集采'}信息`"></uni-title>
			<view flex items-center justify-between pt20px pb-15px border-b>
				<view>商品名称</view>
				<view>{{ name }}</view>
			</view>
<!--
			<view class="border-b" flex items-center justify-between my-5px pb-10px pt20px>
				<view> <text color-red-5>*</text> 品类</view>
				<uni-data-picker :localdata="types" v-model="typesPicked" :map="{text:'title',value:'id'}" 
					:clear-icon="false" placeholder="请选择品类" popup-title="选择品类"
				></uni-data-picker>
			</view>
-->
			<view flex items-center justify-between pt10px pb10px border-b>
				<view> <text color-red-5>*</text> 数量</view>
				<uni-easyinput color-gray v-model="num" :inputBorder="false" placeholder="请输入数量和单位，例如1吨 &nbsp;"></uni-easyinput>
			</view>
<!-- 
			<view flex items-center justify-between pt10px pb10px border-b>
				<view> <text color-red-5>*</text> 时效</view>
				<uni-data-picker :localdata="times" v-model="time" placeholder="请选择时效" popup-title="选择时效"></uni-data-picker>
			</view>
-->
			<view flex items-center justify-between mt10px pb-10px border-b>
				<view> <text color-red-5>*</text> 企业名称</view>
				<uni-easyinput color-gray v-model="enterprise" :inputBorder="false" placeholder="请输入企业名称 &nbsp;"></uni-easyinput>
			</view>

			<view flex items-center justify-between mt10px pb-10px border-b>
				<view> <text color-red-5>*</text> 联系人</view>
				<uni-easyinput color-gray v-model="contact" :inputBorder="false" placeholder="请输入联系人 &nbsp;"></uni-easyinput>
			</view>

			<view flex items-center justify-between mt10px pb-10px border-b>
				<view> <text color-red-5>*</text> 联系电话</view>
				<uni-easyinput color-gray v-model="phone" :inputBorder="false" placeholder="请输入联系电话 &nbsp;"></uni-easyinput>
			</view>

			<view v-if="!type" pt14px pb14px>
				<view pb14px>图片</view>
				<uni-file-picker v-model="imageList" limit="3" @select="selectImage" @delete="deleteImage"  :auto-upload="false"></uni-file-picker>
			</view>

			<view fixed w-94vw bottom-20px left-3vw>
				<button w-full type="primary" rounded-100px :loading="loading" @click="submit">提交</button>
			</view>
		</view>
	
	</view>

</template>

<script setup>
	import api from '@/api'
	import { reactive, ref, computed } from 'vue'
	import { validate } from '@/common/utils'
	import { useUserStore } from '@/stores/user'
	const user = useUserStore()

	const { type, id } = defineProps({
		type: Number,
		id: [String, Number],
		name: String,
	})
	uni.setNavigationBarTitle({
		title: type? '留言' : '我要跟采'
	})

	//集采品类，数量，时效，企业名称，联系人，联系电话
	// const types = ref([])
	// const typesPicked = ref('')
	const num = ref('')
	// const times = ref([])
	// const time = ref('')
	const enterprise = ref('')
	const contact = ref('')
	const phone = ref(user.mobile)

	async function fn(){
		//获取类型
		types.value = await api.getEnterpriseType()
		const baseInfo = await api.getBaseInfo()
		times.value = baseInfo.collective_consign_ageing.map(i => {
			return {
				text: i.name + '天',
				value: i.name
			}
		})
	}
	// fn()

	const imageList = ref([])
	let realImages = []
	const selectImage = async ({tempFiles}) => {
		const file = tempFiles[0]
		const uploadRes = await api.uploadFile(file.url)
		if(uploadRes.images) {
			realImages.push(uploadRes.images)
		}
	}
	const deleteImage = ({index}) => {
		realImages.splice(index,1)
	}

	const submit = async () => {
		// if(!typesPicked.value) {
		// 	uni.showToast({ title: '请选择类型', icon: 'none' })
		// 	return
		// }
		if(!num.value) {
			uni.showToast({ title: '请填写数量', icon: 'none' })
			return
		}
		// if(!time.value) {
		// 	uni.showToast({ title: '请选择时效', icon: 'none' })
		// 	return
		// }
		if(!enterprise.value) {
			uni.showToast({ title: '企业不能为空', icon: 'none' })
			return
		}
		if(!contact.value) {
			uni.showToast({ title: '联系人不能为空', icon: 'none' })
			return
		}
		if(!validate.is_mobile(phone.value)) {
			uni.showToast({ title: '请填写正确的手机号', icon: 'none' })
			return
		}

		const params = {
			// type_id: typesPicked.value,
			num: num.value,
			// ageing: time.value,
			firm_name: enterprise.value,
			contact_name: contact.value,
			contact_phone: phone.value,
			images: JSON.stringify(realImages),
			good_id: id
		}

		const submitRes = await api[type?'sale':'pickup'](params)
		if(submitRes) {
			uni.showToast({ 
				title: '提交成功', 
				icon: 'none',
				success: () => {
					const timer = setTimeout(() => {
						uni.navigateBack()
						clearTimeout(timer)
					}, 2000);
				}
			})
		}
	}
	
</script>

<style lang="scss">
.page-pickupsale {
	
	.border-b{
		border-bottom: 1px solid #eee;
	}
	.uni-easyinput  {
		text-align: right;
	}
	.input-value-border {
		border: none;
	}
}



</style>
